// pages/deposit/vip/index.js
const app = getApp();
const service = require("../../../service/member");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    paddingTop: app.globalData.customNavigation.height,
    showWite: false,
    purchaseRecord:[],
    level: "", // 1: 白银会员 2: 黄金会员 3: 砖石会员
    progressWidth: '5%', // 进度条宽度
    customerId:"",
    info:{},
    balance:0,
    nextLevel:"",
    pageNum: 1, // 当前请求的page
    pageSize: 10, // 请求数据的size
    noData: false,
    nomoredata: false,
  },

  goPay () { // 充值
    wx.navigateTo({
      url: '/pages/deposit/payCenter/index',
    })
  },
  getList(callback) {
    
      service.memberDaybook({
        "membershipRecord":{
            "customerId":this.data.customerId,
            "status":"1"
        },
        "queryRequest":{
          "pageNum": this.data.pageNum,
          "pageSize": this.data.pageSize
        }
    
    }, (res) => {
        console.log(res);
        for (let i = 0; i < res.data.records.length; i++) {
          const element = res.data.records[i];
          if(element.type==1){
            element.name="商城充值"
            element.plus = true
          }else if(element.type==2){
            element.name="收银台充值"
            element.plus = true
          }else if(element.type==3){
            if(element.sourceType==1){
              element.name="微信端"
              element.plus = false
            }else if(element.sourceType==2){
              element.name="商盟订单"
              element.plus = false
            }else if(element.sourceType==3){
              element.name="收银订单"
              element.plus = false
            }else{
              element.name="微信客服订单"
              element.plus = false
            }
          }else{
            element.name="退还"
            element.plus = true
          }
        }
        callback(res);
  
      });
    
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      customerId: wx.getStorageSync("userInfo").id
    })
    wx.showLoading({
      title: '加载中',
    })
    service.getbalance(this.data.customerId,res=>{
      console.log(res.data)
      
    
      this.setData({
        level:res.data && res.data.levelName?res.data.levelName:'暂无等级'
      })
      service.getmemberInfo(this.data.customerId,res1=>{
        wx.hideLoading()
        let nextLevel = ''
        if (!res1.data.nextLevel) {
          nextLevel = ''
        } else {
          nextLevel = res1.data.nextLevel.levelName
        };
        this.setData({
          info:res1.data.memberInformation,
          balance:res1.data.money/100,
          progressWidth:(res1.data.buyMoney/res1.data.levelInformation.maxMoney)*100,
          nextLevel
        })
        console.log(111,this.data.progressWidth)
      })
    })
    this.getList(res=>{
      if(res.code=="00000"){
        if(res.data.records.length==0){
          this.setData({
            purchaseRecord: res.data.records,
            noData: true,
            nomoredata: false
          })
        }else{
          this.setData({
            purchaseRecord: res.data.records,
            noData: false,
            nomoredata: false
          })
        }
      }
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNum: 1,
      noData: false,
    });
    const self = this;
    this.getList(function (res) {
      console.log(res);
      if (res.code == "00000") {
        if (res.data.records.length == 0) {
          self.setData({
            purchaseRecord: res.data.records,
            noData: true,
            nomoredata: false
          })
        } else {
          self.setData({
            purchaseRecord: res.data.records,
            noData: false,
            nomoredata: false
          })
        }

      }

      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.noData) return;
    wx.stopPullDownRefresh();
    const prolist = this.data.purchaseRecord;
    let page = this.data.pageNum;
    this.setData({
      // 每次下拉 page+1
      pageNum: ++page,
    });
    const self = this;

    this.getList(function (res) {
      if (res.data.records.length == 0) {
        self.setData({
          noData: false,
          nomoredata: true
        });
      } else {
        res.data.records.forEach((item) => {

          prolist.push(item);
        });
        self.setData({
          purchaseRecord: prolist,
        });
      }
      // 隐藏加载状态
      // wx.hideNavigationBarLoading()
    });

  },
  onPageScroll: function ({ scrollTop }) { // 页面滚动
    if (scrollTop >= 154) {
      if (!this.data.showWite) {
        this.setData({
          showWite: true
        })
      }
    } else {
      if (this.data.showWite) {
        this.setData({
          showWite: false
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})